package com.caineng.zhaobiao.zbapi.interceptor;

import cn.dev33.satoken.interceptor.SaRouteInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {

    @Autowired
    HttpServletRequest request;


    // 注册Sa-Token的拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //加入拦截器,如果不是安卓,ios,h5等方式登录,则拦截掉
        registry.addInterceptor(new CainengPreLoginInterceptor());

        // 注册路由拦截器，自定义验证规则 
        registry.addInterceptor(new SaRouteInterceptor((req, res, handler) -> {
            System.out.println(DateUtil.now()+"请求的url是================>"+req.getUrl());
            // 登录验证 -- 拦截所有路由，并排除/user/doLogin 用于开放登录 
           // SaRouter.match("/**", "/user/doLogin", () -> StpUtil.checkLogin());
            String api = "/zbapi";
            // 登录验证 -- 排除多个路径
            SaRouter.match(Arrays.asList(api+"/**"),
                    Arrays.asList(api+"/userLogin/**",
                            api+"/index/**",
                            api+"/user/**Sms**",
                            api+"/user/forgot",

                            api+"companyType/findAll",
                            //api+"/company/**",
                            //api+"/zhaobiao/**",
                            api+"/wechat/*", //微信登录
                            api+"/pay/wxPayNotify", //支付之后的回调地址,不能拦截
                            api+"/pay/aliPayNotify",

                            api+"/pay/alipayH5", //支付宝h5支付

                            api+"/wxMpCn/config"),

                            () -> StpUtil.checkLogin());

        })).addPathPatterns("/**");
    }
}
